/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.common.dto.DistributionAuditingDto;
import com.yami.shop.distribution.common.model.DistributionAuditing;
import com.yami.shop.distribution.common.model.DistributionUser;
import com.yami.shop.distribution.common.param.RangeTimeParam;
import com.yami.shop.distribution.common.service.DistributionAuditingService;
import com.yami.shop.distribution.common.service.DistributionUserService;
import com.yami.shop.security.platform.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * @author lgh on 2019/04/01.
 */
@RestController
@RequestMapping("/distribution/distributionAuditing")
@Api(tags = "平台端分销员审核接口")
public class DistributionAuditingController {

    @Autowired
    private DistributionAuditingService distributionAuditingService;
    @Autowired
    private DistributionUserService distributionUserService;

    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:page')")
    @ApiOperation(value = "分页查看分销员审核信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userMobile", value = "分销员手机号码", dataType = "String"),
            @ApiImplicitParam(name = "startExpenseNumber", value = "最低订单数量", dataType = "Integer"),
            @ApiImplicitParam(name = "endExpenseNumber", value = "最高订单数量", dataType = "Integer"),
            @ApiImplicitParam(name = "startPayAmount", value = "最低消费金额", dataType = "Double"),
            @ApiImplicitParam(name = "endPayAmount", value = "最高消费金额", dataType = "Double")
    })
    public ServerResponseEntity<IPage<DistributionAuditingDto>> page(DistributionAuditing distributionAuditing, RangeTimeParam rangeTimeParam,
                                                               String userMobile, Integer startExpenseNumber, Integer endExpenseNumber,
                                                               Double startPayAmount, Double endPayAmount, PageParam<DistributionAuditing> page) {
        IPage<DistributionAuditingDto> list = distributionAuditingService.distributionAuditingPage(page, distributionAuditing, rangeTimeParam, startExpenseNumber, endExpenseNumber, startPayAmount, endPayAmount, userMobile);
        return ServerResponseEntity.success(list);
    }

    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:info')")
    @ApiOperation(value = "根据分销员申请id获取分销员申请信息")
    @ApiImplicitParam(name = "id", value = "分销员申请信息id", dataType = "Long")
    public ServerResponseEntity<DistributionAuditing> info(@PathVariable("id") Long id) {
        DistributionAuditing distributionAuditing = distributionAuditingService.getById(id);
        return ServerResponseEntity.success(distributionAuditing);
    }

    /**
     * 审核
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionAuditing:update')")
    @ApiOperation(value = "审核分销员")
    public ServerResponseEntity<Void> update(@RequestBody @Valid DistributionAuditing distributionAuditing) {
        distributionAuditing.setUpdateTime(new Date());
        distributionAuditing.setModifier(SecurityUtils.getSysUser().getUserId());
        distributionAuditingService.examine(distributionAuditing);
        DistributionUser distributionUser = distributionUserService.getById(distributionAuditing.getDistributionUserId());
        distributionUserService.removeCacheByUserIdAndShopId(distributionUser.getUserId(), distributionAuditing.getShopId());
        distributionUserService.removeCacheByCardNo(distributionUser.getCardNo());
        return ServerResponseEntity.success();
    }
}
